package com.googlecode.imule.redundant;

/**
 * Parse XML using digest, add the common fields to the system
 * @author owen
 *
 */

import org.xml.sax.SAXException;
import org.apache.commons.digester.Digester;

import java.io.File;
import java.io.IOException;

public class Components {
	
	/**
	 * Prints the contact information to standard output.
	 * 
	 * @param field
	 *            the <code>Field</code> to print out
	 */
	
	public void printComponents(Fields f) {
		System.out.println("Type: " + f.getType());
		System.out.println("Name: " + f.getName());
		System.out.println("Description: " + f.getDescription());
		System.out.println("Protocol: " + f.getCom_protocol());
		//System.out.println("Getters: " + f.getGetters());
		//System.out.println("Setters: " + f.getSetters());
	}

	
	/**
	 * Parses the data
	 * @throws SAXException 
	 */
	public static void main(String[] args) throws IOException, SAXException {
		Fields f = new Fields();
		Digester d = new Digester();
		d.setValidating(false);

		d.addObjectCreate("components", Components.class);
		d.addObjectCreate("fields", Fields.class);
		
		// set type property of Fields instance when 'type' attribute is found
        d.addSetProperties("components/fields",         "type", "type" );
		
		// set fields for the components class
		d.addCallMethod("components/fields/name", "fields.setName", 0);
		d.addCallMethod("components/fields/description", "fields.setDescription", 0);
		d.addCallMethod("components/fields/com_protocol", "fields.setCom_protocol", 0);
			
		// call 'addField' method when the next 'components/fields' pattern is seen
        d.addSetNext("components/fields",               "printComponents" );
        
		// Parse the xml
		Components components = (Components) d.parse(
				new File(
						"C:/Documents and Settings/Neil/My Documents/College/Distributed Systems/Project/imule/src/com/googlecode/imule/data/components.xml"));
	}
	
	/**
	 * Variables, getters and setters
	 * 
	 */
    public static class Fields
    {
    	private String type;
    	private String name;
    	private String description;
    	private String com_protocol;
    	private Method[] getters;
    	private Method[] setters;
    	
    	public String getType(){
    		return type;
    	}
    	
    	public void setType(String type){
    		this.type = type;
    	}

    	public String getName() {
    		return name;
    	}

    	public void setName(String name) {
    		this.name = name;
    	}

    	public String getDescription() {
    		return description;
    	}

    	public void setDescription(String description) {
    		this.description = description;
    	}

    	public String getCom_protocol() {
    		return com_protocol;
    	}

    	public void setCom_protocol(String com_protocol) {
    		this.com_protocol = com_protocol;
    	}

    	public Method[] getGetters() {
    		return getters;
    	}

    	public Method[] getSetters() {
    		return setters;
    	}
    }
	


}